package com.imodia.checkoo.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.hsq.commons.util.db.GenericIbatisDao;
import com.hsq.commons.util.db.SqlParam;
import com.hsq.commons.util.db.annotation.SqlSpace;
import com.imodia.checkoo.bean.CmsDataDictionaryBean;
import com.imodia.checkoo.bean.CmsJobAreaBean;
import com.imodia.checkoo.service.CmsJobAreaService;

@SqlSpace(spaceName="cmsjobarea")
@Service("cmsJobAreaService")
public class CmsJobAreaServiceImpl  extends GenericIbatisDao<CmsJobAreaBean, Integer> implements CmsJobAreaService{
	
	private static final Log log = LogFactory.getLog(CmsJobAreaServiceImpl.class);
	
	

	
	public  List<CmsJobAreaBean> query(int jobId){
		SqlParam param = new SqlParam();
		param.andEqualTo("job_id", String.valueOf(jobId));
		return super.list(param);
	}
	

	public void setSelected(List<CmsDataDictionaryBean> dataDictionaryList,int jobId){
		List<CmsJobAreaBean> jobAreaList  = query(jobId);
		for(CmsDataDictionaryBean dataDictionary:dataDictionaryList){
			for(CmsJobAreaBean jobArea : jobAreaList ){
				if(dataDictionary.getId() == jobArea.getAreaId()){
					dataDictionary.setSelected(1);
				}
			}
		}
	}
	
	
	public void save(List<CmsJobAreaBean> area,int jobId){
		SqlParam param = new SqlParam();
		param.andEqualTo("job_id", String.valueOf(jobId));
		super.delete(param);
		super.batchInsert(area);
	}
	
	public void save(String areaIds,int jobId){
		List<CmsJobAreaBean> list = new ArrayList<CmsJobAreaBean>();
		if(areaIds!=null && areaIds.trim().length()>0){
			String[] areaId = areaIds.split(",");
			for(String s:areaId){
				if(s!=null && s.matches("\\d+")){
					CmsJobAreaBean jobArea = new CmsJobAreaBean();
					jobArea.setJobId(jobId);
					jobArea.setAreaId(Integer.parseInt(s));
					list.add(jobArea);
				}
			}
		}
		save(list,jobId);
	}
	
	public void save(String[] areaIds,int jobId){
		List<CmsJobAreaBean> list = new ArrayList<CmsJobAreaBean>();
		if(areaIds!=null){
			for(String s:areaIds){
				if(s!=null && s.matches("\\d+")){
					CmsJobAreaBean jobArea = new CmsJobAreaBean();
					jobArea.setJobId(jobId);
					jobArea.setAreaId(Integer.parseInt(s));
					list.add(jobArea);
				}
			}
		}
		save(list,jobId);
	}

	
	
}
